#include "playbackshortcutsettings.h"
#include <QTest>

PlaybackShortcutSettings::PlaybackShortcutSettings(QWidget * parent) : SettingsMenuItem(parent)
{
    initializeButtons();

}

void PlaybackShortcutSettings::initializeButtons() {
    QVBoxLayout * mainLayout=new QVBoxLayout(this);
    setLayout(mainLayout);
    /*QLayout * currentLayout=new QHBoxLayout();
    currentLayout->addWidget(new QLabel("Up",this));
    KeyDialog * dialog=new KeyDialog(this);
    dialog->setOneKey(true);
    connect(dialog,SIGNAL(okSelected()),this,SLOT(show()));
    dialog->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    dialog->hide();
    dialog->setMinimumSize(300,200);
    changeUp=new Button(QIcon(), "", this, dialog);
    currentLayout->addWidget(changeUp);
    addRow(changeUp);

    mainLayout->addItem(currentLayout);
    mainLayout->setAlignment(currentLayout,Qt::AlignTop);*/

    QLayout * currentLayout=new QHBoxLayout();
    QLabel * currentLabel=new QLabel("Key Name",this);
    QFont font=QFont();
    font.setUnderline(true);
    font.setBold(true);
    currentLabel->setFont(font);
    currentLayout->addWidget(currentLabel);
    currentLabel=new QLabel("Key Binding",this);
    currentLabel->setFont(font);
    currentLayout->addWidget(currentLabel);
    currentLabel=new QLabel("Click To Change",this);
    currentLabel->setFont(font);
    currentLayout->addWidget(currentLabel);
    layout()->addItem(currentLayout);
    layout()->setAlignment(currentLayout,Qt::AlignTop);

    buttons=QList<Button *>();

    changePlay=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changePlay->setIconSize(QSize(256,48));
    playLabel=new QLabel(this);
    addButton("Play / Pause",playLabel,changePlay);

    changeStop=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changeStop->setIconSize(QSize(256,48));
    stopLabel=new QLabel(this);
    addButton("Stop",stopLabel,changeStop);

    changeFF=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changeFF->setIconSize(QSize(256,48));
    ffLabel=new QLabel(this);
    addButton("Fast Forward",ffLabel,changeFF);

    changeRW=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changeRW->setIconSize(QSize(256,48));
    rwLabel=new QLabel(this);
    addButton("Rewind",rwLabel,changeRW);

    changeSkip=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changeSkip->setIconSize(QSize(256,48));
    skipLabel=new QLabel(this);
    addButton("Next",skipLabel,changeSkip);

    changePrev=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changePrev->setIconSize(QSize(256,48));
    prevLabel=new QLabel(this);
    addButton("Previous",prevLabel,changePrev);

    changeFullS=new Button(QIcon(":/imgs/3F00Button_ChangeKey.png"), "", this);
    changeFullS->setIconSize(QSize(256,48));
    fullScreenLabel=new QLabel(this);
    addButton("Enter/Exit Full screen",fullScreenLabel,changeFullS);

    QHBoxLayout * horizontalLayout=new QHBoxLayout();
    QList<Button *> * list=new QList<Button *>();
    saveButton = new Button(QIcon(":/imgs/3F00Button_Save.png"),"",this,NULL);
    saveButton->setIconSize(QSize(110,64));
    horizontalLayout->addWidget(saveButton);
    list->append(saveButton);
    cancelButton = new Button(QIcon(":/imgs/3F00Button_Cancel.png"),"",this);
    cancelButton->setIconSize(QSize(110,64));
    horizontalLayout->addWidget(cancelButton);
    list->append(cancelButton);
    addRow(list);
    mainLayout->addItem(horizontalLayout);
    foreach(Button * button, buttons) {
        addDialog(button);
    }
    loadBindings();
}

void PlaybackShortcutSettings::addButton(QString name, QLabel * label, Button * button) {
    QLayout * currentLayout=new QHBoxLayout();
    currentLayout->addWidget(new QLabel(name,this));
    currentLayout->addWidget(label);
    currentLayout->addWidget(button);
    addRow(button);
    button->setItem(label);
    layout()->addItem(currentLayout);
    layout()->setAlignment(currentLayout,Qt::AlignTop);
    buttons.append(button);
}

void PlaybackShortcutSettings::addDialog(Button *button) {
    KeyDialog * dialog=new KeyDialog(this);
    dialog->setOneKey(true);
    connect(dialog,SIGNAL(okSelected(KeyDialog *)),this,SLOT(activate()));
    connect(dialog,SIGNAL(okSelected(KeyDialog *)),this,SLOT(updateShortcut(KeyDialog*)));
    dialog->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint |Qt::WindowStaysOnTopHint);
    dialog->hide();
    dialog->setMinimumSize(300,200);
    QLabel * label=qobject_cast<QLabel *>(button->getItem());
    hash.insert(dialog,label);
    button->setItem(dialog);
}

void PlaybackShortcutSettings::save() {
    save("play",changePlay);
    save("stop",changeStop);
    save("ff",changeFF);
    save("rw",changeRW);
    save("skip",changeSkip);
    save("previous",changePrev);
    save("fullscreen",changeFullS);
    KeyBindings::loadKeyBindings();
    emit completed();
}

void PlaybackShortcutSettings::save(QString name, Button *button) {
    KeyDialog * dialog=qobject_cast<KeyDialog *>(button->getItem());

    if(dialog) {
        qDebug() << getModifierString(dialog->getModifiers());
        QString query="UPDATE shortcuts SET key='"+dialog->getText()+"', modifierKeys='"+QString::number(dialog->getModifiers())+"' WHERE function='"+name+"'";
        Querier::getResult(query);
        qDebug() <<query;
    }
}

void PlaybackShortcutSettings::cancel() {
    loadBindings();
    emit completed();
}

void PlaybackShortcutSettings::buttonSelected() {
    if(getCurrentButton()==saveButton) {
        save();
    } else if(getCurrentButton()==cancelButton) {
        cancel();
    }
    else {
        QObject * object=getCurrentButton()->getItem();
        KeyDialog * keyDialog = qobject_cast<KeyDialog *>(object);
        qDebug() <<"key dialog "<<keyDialog;
        if(keyDialog) {
            keyDialog->move(300,100);
            deactivate();
            keyDialog->show();
        }
    }
}

void PlaybackShortcutSettings::reshowComponents() {

}

void PlaybackShortcutSettings::loadBindings() {
    ((KeyDialog *)changePlay->getItem())->setKey(KeyBindings::playKey[0],KeyBindings::playKey[1]);
    ((KeyDialog *)changeStop->getItem())->setKey(KeyBindings::stopKey[0],KeyBindings::stopKey[1]);
    ((KeyDialog *)changeFF->getItem())->setKey(KeyBindings::ffKey[0],KeyBindings::ffKey[1]);
    ((KeyDialog *)changeRW->getItem())->setKey(KeyBindings::rwKey[0],KeyBindings::rwKey[1]);
    ((KeyDialog *)changeSkip->getItem())->setKey(KeyBindings::skipKey[0],KeyBindings::skipKey[1]);
    ((KeyDialog *)changePrev->getItem())->setKey(KeyBindings::previousKey[0],KeyBindings::previousKey[1]);
    ((KeyDialog *)changeFullS->getItem())->setKey(KeyBindings::fullscreenKey[0],KeyBindings::fullscreenKey[1]);
    foreach(KeyDialog * dialog, hash.keys()) {
        updateShortcut(dialog);
    }
}

void PlaybackShortcutSettings::saveTest() {
    //QTest::mouseMove(volumeUp);
    //emit buttonEntered(volumeUp);
//    QTest::qWait(500);
//    QTest::mouseClick(volumeUp,Qt::LeftButton);
//    QTest::qWait(1000);
//    KeyDialog * dialog=(KeyDialog *)volumeUp->getItem();
//    //QTest::qWaitForWindowShown(dialog);
//    QTest::keyPress(dialog,Qt::Key_7,Qt::NoModifier);
//    QTest::qWait(1000);
//    QTest::mouseMove(saveButton);
//    emit buttonEntered(saveButton);
//    QTest::qWait(500);
//    QTest::mouseClick(saveButton,Qt::LeftButton);

}

void PlaybackShortcutSettings::assertSaved() {
//    QCOMPARE(KeyBindings::volumeUpKey[0],(int)Qt::Key_7);
//    QCOMPARE(KeyBindings::volumeUpKey[1],(int)Qt::NoModifier);
//    QTest::mouseMove(volumeUp);
//    emit buttonEntered(volumeUp);
//    QTest::qWait(500);
//    QTest::mouseClick(volumeUp,Qt::LeftButton);
//    QTest::qWait(1000);
//    KeyDialog * dialog=(KeyDialog *)volumeUp->getItem();
//    QTest::keyPress(dialog,Qt::Key_Plus,Qt::ShiftModifier);
//    QTest::mouseMove(saveButton);
//    emit buttonEntered(saveButton);
//    QTest::qWait(500);
//    QTest::mouseClick(saveButton,Qt::LeftButton);
}

void PlaybackShortcutSettings::cancelTest() {
//    QTest::mouseMove(volumeUp);
//    emit buttonEntered(volumeUp);
//    QTest::qWait(500);
//    QTest::mouseClick(volumeUp,Qt::LeftButton);
//    QTest::qWait(1000);
//    KeyDialog * dialog=(KeyDialog *)volumeUp->getItem();
//    QTest::keyPress(dialog,Qt::Key_Plus,Qt::NoModifier);
//    QTest::mouseMove(cancelButton);
//    emit buttonEntered(cancelButton);
//    QTest::qWait(1000);
//    QTest::mouseClick(cancelButton,Qt::LeftButton);
}

void PlaybackShortcutSettings::assertCancelled() {
    QCOMPARE(KeyBindings::volumeUpKey[0],(int)Qt::Key_Plus);
    QCOMPARE(KeyBindings::volumeUpKey[1],(int)Qt::ShiftModifier);
}

QString PlaybackShortcutSettings::getModifierString(int modifiers) {
    QString result="";
    QFlags<Qt::KeyboardModifier> modifierKeys=QFlags<Qt::KeyboardModifier>(modifiers);
    if(modifierKeys.testFlag(Qt::ShiftModifier)) {
        result+="Shift + ";
    }
    if(modifierKeys.testFlag(Qt::ControlModifier)) {
        #if defined(Q_OS_MAC)
            result+="Command + ";
        #else
                result+="Ctl + ";
        #endif
    }

    if(modifierKeys.testFlag(Qt::MetaModifier)) {
        #if defined(Q_OS_MAC)
            result+="Ctl + ";
        #else
            result+="Windows + ";
        #endif
    }
    if(modifierKeys.testFlag(Qt::AltModifier)) {
        result+="Alt + ";
    }
    return result;
}

void PlaybackShortcutSettings::updateShortcut(KeyDialog *dialog) {
    QLabel * label=hash.value(dialog);
    QString key="";
    int value=dialog->getText().toInt();
    switch(value) {
        case Qt::Key_Up:
            key+="Up";
        break;
        case Qt::Key_Down:
            key+="Down";
        break;
        case Qt::Key_Left:
            key+="Left";
        break;
        case Qt::Key_Right:
            key+="Right";
        break;
        case Qt::Key_Backspace:
            key+="Backspace";
        break;
        case Qt::Key_Delete:
            key+="Delete";
        break;
        case Qt::Key_Enter:
            key+="Enter";
        break;
        case Qt::Key_Space:
            key+="Space";
        break;
        case Qt::Key_Return:
            key+="Enter";
        break;
        default:
            key+=(char)value;
            break;
    }

    label->setText(getModifierString(dialog->getModifiers())+"'"+key+"'");
}
